package com.lb.dtregister;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("DtRegister");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //调用MainActivity中的方法(stringFromJNI()方法)...
        TextView tv1 = (TextView) findViewById(R.id.sample_text1);
        tv1.setText(stringFromJNI());

        //调用MainActivity中的方法(setString()方法)...
        TextView tv2 = (TextView) findViewById(R.id.sample_text2);
        tv2.setText(setString("Hello,欢迎来到JNI的世界..."));

        //调用HongBao类中的java方法<sub()方法>
        TextView tv3 = (TextView) findViewById(R.id.sample_text3);
        tv3.setText("测试HongBao类java中sub(5,3)方法相减的结果："+HongBao.sub(5,3));

        //调用HongBao类中的java native方法<add()方法>
        TextView tv4 = (TextView) findViewById(R.id.sample_text4);
        tv4.setText("测试HongBao类native的add(5,3)方法相加的结果："+HongBao.add(3,5));


        Button chgMethod = (Button) findViewById(R.id.chgMethod);
        chgMethod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    Method m = MainActivity.class.getMethod("ret1");
                    chgM(m);
                    chgM2();
                }catch (Exception e)
                {
                    e.printStackTrace();
                }


                Log.d("DrRegister","this value is ["+ret1()+"]");
            }
        });

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public static native String stringFromJNI();

    //添加的一个带参有返回值的native函数
    public native String setString(String str);

    public int ret1()
    {
        return 1;
    }

    public int ret2()
    {
        return 2;
    }

    public native void chgM(Method r);

    public native void chgM2();
}
